<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: Info.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");


class Info extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name = 'multilang_table';
	
	function __construct() {
		global $db;
		parent::__construct();
		$this->db =$db;
	}
	
	function getList() {
		global $db,$config,$multiLang;		
		//temp variables
		$g_admin_language_id=1;
		//get session info for this page
		$session_prefix = getPageName();

		//$translate_lang_id = $_COOKIE["s_admin_{$session_prefix}_language_id"];

		if (isset($_REQUEST['language_id']))
		{
			$_SESSION["s_admin_{$session_prefix}_language_id"] = trim($_REQUEST['language_id']);
		} 
		if (!empty($_SESSION["s_admin_{$session_prefix}_language_id"]))
			$translate_lang_id = $_SESSION["s_admin_{$session_prefix}_language_id"];
		else
			$translate_lang_id=1;

		
		if ($_GET['dir']=="ASC" || $_GET['dir']=="DESC" ) {
			if ($_GET['sort']) {
				$orderby = " order by  ".$_GET['sort'] ." ".$_GET['dir'];	
			}	
		}

		/*$sql_count = "select count(*) as total 
						from  module_permission mp,multilang_table mut ,multilang_info mui 
						where  mp.multilang_table_id=mui.key_id	
							and mui.multilang_table_id=mut.multilang_table_id
							and mui.language_id={$g_admin_language_id}  
							and mp.state=1					
							and mut.table_name='module_permission'
							and mp.module_permission_type in (0,1) 
							$condition ";	*/
		$sql_count = "select count(*) as total 
				from multilang_table mt, multilang_info mi, language l 
				where 
					mt.multilang_table_id=mi.multilang_table_id
					and  mi.language_id=l.language_id
					and mi.language_id={$g_admin_language_id}
					$condition 
				group by mt.table_name";

		//echo $sql_count;
		
		$stmt='';     $stmt=$db->query($sql_count);
		$rs_count =$stmt->fetchAll();
		$rows = $rs_count[0]["total"];
		$pager = getPagerParam($session_prefix);
		
		$sql = "select mt.table_name table_name, mt.multilang_table_id ,
					mt.description description
				from multilang_table mt, multilang_info mi, language l 
				where 
					mt.multilang_table_id=mi.multilang_table_id
					and  mi.language_id=l.language_id
					and mi.language_id={$g_admin_language_id}
					$condition   	
					group by mt.table_name
                    $orderby
					limit $pager->_offsize,$pager->_limit 
		";
		//echo $sql;
		//exit();
		$stmt='';     $stmt=$db->query($sql);
		$moduleRS =$stmt->fetchAll();

		foreach ((array)$moduleRS as $key => $value) {
			$moduleArr['multilang_table_id']=$value['multilang_table_id'];
			$moduleArr['description']=$value['description'];

			//get english var's totel
			$countSql="select count(*) as varCount
				 from multilang_table mt, multilang_info mi, language l 
				where 
					mt.multilang_table_id=mi.multilang_table_id
					and  mi.language_id=l.language_id
					and mi.language_id={$g_admin_language_id}
					and mi.multilang_table_id={$value['multilang_table_id']}
			";
			$stmt='';     $stmt=$db->query($countSql);
			$countRS =$stmt->fetchAll();

			if ($countRS)
				$moduleArr['varCount']=$countRS[0]['varCount'];
			else
				$moduleArr['varCount']=0;

			
			//get other language var's totel
			$countSql="select count(*) as varCount2
				 from multilang_table mt, multilang_info mi, language l 
				where 
					mt.multilang_table_id=mi.multilang_table_id
					and  mi.language_id=l.language_id
					and mi.language_id={$translate_lang_id}
					and mi.multilang_table_id={$value['multilang_table_id']}
			";
			//echo $countSql;
			//exit(); 
			$stmt='';     $stmt=$db->query($countSql);
			$countRS =$stmt->fetchAll();
			if ($countRS)
				$moduleArr['varCount2']=$countRS[0]['varCount2'];
			else
				$moduleArr['varCount2']=0;
		
			$moduleArr['action']=backendActionLink($multiLang->g_view,"list_view_link.gif","","#"," onclick='checkLang(\"{$config['web']['baseurl']}multilang/info/content/?\",\"{$value['multilang_table_id']}\")' ");		
			$moduleData[]=$moduleArr;
		} 

		$listData=array(
			"total"=>$rows,
			"rs"=>$moduleData
		);
		return $listData;		
	}

	// Content List
	function getContentList() {
		global $db,$config;		
		$session_prefix = getPageName();

		$tran_language_id=$_GET['language_id'];
		$tran_multilang_table_id=$_GET['multilang_table_id'];

		if(!$tran_language_id )	{	
			$tran_language_id=1;
		}	
		if (isset($_REQUEST['search'])){
			$_SESSION["s_admin_{$session_prefix}_content"] = trim($_REQUEST['search']);
		} 
		$s_keyword = $_SESSION["s_admin_{$session_prefix}_content"];
		
		if($s_keyword)	{
			$condition .= " and ( t2.content like '%{$s_keyword}%' or t3.content like '%{$s_keyword}%' ) ";	
		}
		
		
		if (isset($_GET['multilang_table_id'])){
			if ($tran_multilang_table_id!="all") {
				$condition .= " and t2.multilang_table_id= '{$tran_multilang_table_id}' ";	
			}			
		} 
		
		$orderby = " order by ";
		if ($_GET['dir']=="ASC" || $_GET['dir']=="DESC" ){
		   $orderby.=$_GET['sort']." ".$_GET['dir'].",";		
		}
		$orderby.= " updated_time_tran  desc  ";		

		$sql_count="select count(*) as total 
			from multilang_info t2
				 left join multilang_info t3 
				 on t2.multilang_table_id =      t3.multilang_table_id 
				 and t3.language_id = 2 
				 and t2.key_id = t3.key_id
			where t2.language_id = 1
			$condition  "; 

		$stmt='';     $stmt=$db->query($sql_count);
		$rs_count =$stmt->fetchAll();
		$rows = $rs_count[0]["total"];

		$pager = getpagerparam ( $session_prefix );
		$sql_limit = " limit $pager->_offsize,$pager->_limit ";
					
		$sql="select t2.content content_en,
				t3.content content_tran,
				t2.updated_time updated_time_en,
				t3.updated_time updated_time_tran,
				t2.multilang_table_id table_id,
				t2.key_id key_id,
               	t1.description description
			from multilang_info t2
			 left join multilang_info t3 
				 on t2.multilang_table_id =      t3.multilang_table_id 
				 and t3.language_id = 2  
				 and t2.key_id = t3.key_id
			  inner join multilang_table t1
                 on t1.multilang_table_id=t2.multilang_table_id	
			where t2.language_id = 1
			$condition   $orderby  $sql_limit ";
		
		$stmt='';     $stmt=$db->query($sql);
		$rs =$stmt->fetchAll();

		$listData=array(
			"total"=>$rows,
			"rs"=>$rs
		);
		return $listData;		
	}
	

	function delete($id)
	{		
		$sql_pre="DELETE multilang_content,multilang_translate " .
			" from multilang_translate LEFT JOIN multilang_content ".
			" ON multilang_content.multilang_content_id=multilang_translate.multilang_content_id  ".
			" WHERE multilang_translate.multilang_content_id=";
		$rows_affected=$this->db->query($sql_pre.$id);

		return $rows_affected;
	}
	
	/*
	 * Get all table name list
	 */
	function getAll(){
		global $db,$config,$multiLang;		
		$g_admin_language_id=1;
		$sql = "select mt.table_name table_name, mt.multilang_table_id ,
					mt.description description
				from multilang_table mt, multilang_info mi, language l 
				where 
					mt.multilang_table_id=mi.multilang_table_id
					and  mi.language_id=l.language_id
					and mi.language_id={$g_admin_language_id}
					group by mt.table_name
		";
		$stmt='';     $stmt=$db->query($sql);
		$rs=$stmt->fetchAll();
		return $rs;
	}
	
	/*
	 * Get all table name select  
	 */
	function getSelect(){
		$rs=$this->getAll();
		foreach($rs as $v){
			$arr[$v["multilang_table_id"]]=$v["description"];
		}
		return $arr;
	}
}
